# -*- coding: utf-8 -*-
import logging
import json

import requests
import datetime
from urllib.parse import quote, unquote
import os 
import time 
import datetime


import requests
from bs4 import BeautifulSoup
import json
import urllib.request


class weChat_listening:
    def requestURL(self):
        wechat_URL = 'https://mp.weixin.qq.com/cgi-bin/announce?action=getannouncementlist&lang=zh_CN'
        wechat_Header = {
            "accept": "*/*",
            "accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
            "cache-control": "no-cache",
            "pragma": "no-cache",
            "sec-ch-ua": "\"Google Chrome\";v=\"113\", \"Chromium\";v=\"113\", \"Not-A.Brand\";v=\"24\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin"
        }
        
        responseValue = requests.get(url=wechat_URL,headers=wechat_Header)
        # print(responseValue.text)
        beautifulSoup = BeautifulSoup(responseValue.text, 'html.parser')
        resultStrring = beautifulSoup.find('li', class_ = 'mp_news_item')
        '''
        <li class="mp_news_item"> <a href="/cgi-bin/announce?action=getannouncement&amp;announce_id=11694692148T7z84&amp;version=&amp;lang=zh_CN" target="_blank"> <strong>关于小程序隐私保护指引设置的再次公告                                        <i class="icon_common new"></i> </strong> <span class="read_more">2023-09-14</span> </a> </li>
        '''
        item_title = resultStrring.select('strong')[0].get_text()
        item_time = resultStrring.select('span')[0].get_text()
        yes_time = self.timeTransform()
        print(yes_time)
        if item_time in str(yes_time):
            print('11')
            notification_class().notificationToken("微信公众号系统公告", item_title)
        else:
            print('22')
            # notification_class().notificationToken(item_title, item_time)
        
        
        # if resultStrring:
        #    resultSimple = resultStrring.find('li', id = 'mp_news_item')

    def timeTransform(self):
        today = datetime.date.today()
        # 3.计算昨天的日期
        yesterday = today - datetime.timedelta(days=1)
        return yesterday



class notification_class:
    # 钉钉机器人的调用
    def dingdingTalk(self,msg):
        HEADERS = {"Content-Type": "application/json;charset=utf-8"}
        key = "e9b59afdcad471cd70b8e4016f2752a03084d66c34abea961f2ebf8a3d785a30"
        url = "https://oapi.dingtalk.com/robot/send?access_token=%s" % key
        data_info = {
            "msgtype": "text",
            "text": {
                # "content": "Log: \n" + msg
                "content": msg
            },
            "isAtAll": True
        }

        value = json.dumps(data_info)
        response = requests.post(url, data=value, headers=HEADERS)

    def notificationToken(self,titleMsg, message):
    # tkoen = 'WwuUQD5ZNv7Aq9A67AswCN'#晓粉的
        tkoen = 'eJYzQEezv5JtJeR6mDQfVG'  # 我的
        first = urllib.parse.quote(titleMsg, 'utf-8')
        second = urllib.parse.quote(message, 'utf-8')
        nowTmp = 'https://api.day.app/{}/{}/{}'.format(tkoen, first, second)
        print(nowTmp)
        res = requests.post(nowTmp)

        if res.json()['code'] == 200:
            print('发送成功')
        else:
            print(res.text)
 

def handler(event, context):
    d1 = datetime.datetime.now();
    d2 = datetime.datetime(2021, 2, 17);
    # d3 = datetime.datetime(2020, 2, 9);
    d4 = (d1 - d2).days + 1; # 在一起多久
    # d5 = (d1 - d3).days + 1; # 认识多久
    msg = 'Tips: 认识晓粉已经' + str(d4) + '天'
    notification_class().notificationToken(titleMsg='纪念日', message=msg)
    weChat_listening().requestURL()
  

if __name__ == '__main__':
    d1 = datetime.datetime.now();
    d2 = datetime.datetime(2021, 2, 17);
    # d3 = datetime.datetime(2020, 2, 9);
    d4 = (d1 - d2).days + 1; # 在一起多久
    # d5 = (d1 - d3).days + 1; # 认识多久
    msg = 'Tips: 认识晓粉已经' + str(d4) + '天'
    notification_class().notificationToken(titleMsg='纪念日', message=msg)
    weChat_listening().requestURL()
# def handler(event, context):
#   # evt = json.loads(event)
#   logger = logging.getLogger()
#   logger.info('hello world')
#   return 'hello world'